package com.jg.visitor.controller.common;

import com.jg.visitor.GlobalConst;
import com.jg.visitor.common.bean.ResponseBean;
import com.jg.visitor.common.bean.ServiceCode;
import com.jg.visitor.common.utils.CheckParamUtils;
import com.jg.visitor.common.utils.LoginHelper;
import com.jg.visitor.controller.dto.UserDTO;
import com.jg.visitor.controller.vo.UserLoginVO;
import com.jg.visitor.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Api(tags = "账户相关接口")
@Slf4j
@RestController
@RequestMapping("/api/account")
public class UserController {

    @Resource
    private UserService userService;

    @ApiOperation("管理员审核员登录")
    @PostMapping("/login")
    public ResponseBean login(@RequestBody UserLoginVO user){
        if(CheckParamUtils.isAnyStringEmpty(user.getUsername(),user.getPassword())){
            return ResponseBean.error(ServiceCode.BAD_REQUEST);
        }

        UserDTO userDTO = userService.login(user);
        if(userDTO != null){
            return ResponseBean.success(userDTO);
        }

        return ResponseBean.error(ServiceCode.LOGIN_FAIL);
    }

    @ApiOperation("管理员审核员退出登录")
    @GetMapping("/logout")
    public ResponseBean logout(){
        //退出登录缓存失效
        LoginHelper.loginOutUser();

        return ResponseBean.success();
    }

    @ApiOperation("小程序进入后初始化openid")
    @GetMapping("wechat-init/{jsCode}")
    @ApiImplicitParam(name = "jsCode",value = "通过此code才能获取到唯一openid",dataTypeClass = String.class)
    public ResponseBean init(@PathVariable String jsCode){
        UserDTO userDTO = userService.wxLogin(jsCode);

        return ResponseBean.success(userDTO);
    }

}
